#!/bin/bash

###############################################################################
# 军事数据加工平台 - 首次部署脚本
# 
# 功能：
# 1. 检查环境依赖
# 2. 安装必要的软件包
# 3. 配置数据库
# 4. 初始化项目
# 5. 配置 Nginx 和 PM2
#
# 使用方法：
# 1. chmod +x first-deploy.sh
# 2. sudo ./first-deploy.sh
#
# 注意：此脚本仅在首次部署时运行一次
###############################################################################

set -e

# 颜色定义
GREEN='\033[0;32m'
BLUE='\033[0;34m'
YELLOW='\033[1;33m'
RED='\033[0;31m'
NC='\033[0m'

# 配置项
PROJECT_DIR="/var/www/janes"
FRONTEND_DIR="/var/www/janes-frontend"
APP_NAME="janes-backend"

print_success() {
    echo -e "${GREEN}✓ $1${NC}"
}

print_info() {
    echo -e "${BLUE}ℹ $1${NC}"
}

print_warning() {
    echo -e "${YELLOW}⚠ $1${NC}"
}

print_error() {
    echo -e "${RED}✗ $1${NC}"
}

print_step() {
    echo -e "\n${BLUE}==== $1 ====${NC}\n"
}

# 检查是否以 root 权限运行
if [ "$EUID" -ne 0 ]; then 
    print_error "请使用 sudo 运行此脚本"
    exit 1
fi

echo "======================================"
echo "军事数据加工平台 - 首次部署"
echo "======================================"

# 1. 更新系统
print_step "[1/10] 更新系统"
apt update && apt upgrade -y
print_success "系统已更新"

# 2. 安装基础工具
print_step "[2/10] 安装基础工具"
apt install -y curl wget git build-essential
print_success "基础工具已安装"

# 3. 安装 Node.js
print_step "[3/10] 安装 Node.js"
if ! command -v node &> /dev/null; then
    curl -fsSL https://deb.nodesource.com/setup_18.x | bash -
    apt install -y nodejs
    print_success "Node.js 已安装: $(node -v)"
else
    print_warning "Node.js 已存在: $(node -v)"
fi

# 4. 安装 pnpm
print_step "[4/10] 安装 pnpm"
if ! command -v pnpm &> /dev/null; then
    npm install -g pnpm
    print_success "pnpm 已安装: $(pnpm -v)"
else
    print_warning "pnpm 已存在: $(pnpm -v)"
fi

# 5. 安装 PM2
print_step "[5/10] 安装 PM2"
if ! command -v pm2 &> /dev/null; then
    npm install -g pm2
    pm2 startup
    print_success "PM2 已安装"
else
    print_warning "PM2 已存在"
fi

# 6. 安装 MySQL
print_step "[6/10] 安装 MySQL"
if ! command -v mysql &> /dev/null; then
    apt install -y mysql-server
    systemctl start mysql
    systemctl enable mysql
    print_success "MySQL 已安装"
    print_warning "请手动运行: sudo mysql_secure_installation"
else
    print_warning "MySQL 已存在"
fi

# 7. 安装 Nginx
print_step "[7/10] 安装 Nginx"
if ! command -v nginx &> /dev/null; then
    apt install -y nginx
    systemctl start nginx
    systemctl enable nginx
    print_success "Nginx 已安装"
else
    print_warning "Nginx 已存在"
fi

# 8. 创建项目目录
print_step "[8/10] 创建项目目录"
mkdir -p $PROJECT_DIR
mkdir -p $FRONTEND_DIR
mkdir -p $PROJECT_DIR/uploads/{avatars,temp,thumbnails}
mkdir -p $PROJECT_DIR/templates
mkdir -p $PROJECT_DIR/logs
mkdir -p /var/backups/janes

print_success "项目目录已创建"

# 9. 设置目录权限
print_step "[9/10] 设置目录权限"
chown -R $SUDO_USER:$SUDO_USER $PROJECT_DIR
chown -R www-data:www-data $FRONTEND_DIR
chmod -R 755 $PROJECT_DIR/uploads
chmod -R 755 $PROJECT_DIR/templates

print_success "目录权限已设置"

# 10. 配置防火墙
print_step "[10/10] 配置防火墙"
if command -v ufw &> /dev/null; then
    ufw allow 80/tcp
    ufw allow 443/tcp
    print_success "防火墙规则已添加"
else
    print_warning "UFW 未安装，跳过防火墙配置"
fi

# 完成
echo ""
echo "======================================"
echo "首次部署准备完成！"
echo "======================================"
echo ""
print_info "下一步操作："
echo ""
echo "1. 上传代码到服务器："
echo "   git clone <your-repo> $PROJECT_DIR"
echo "   或使用 scp/rsync 上传"
echo ""
echo "2. 配置环境变量："
echo "   cd $PROJECT_DIR"
echo "   cp env.example .env"
echo "   nano .env"
echo ""
echo "3. 配置数据库："
echo "   - 运行: sudo mysql_secure_installation"
echo "   - 创建数据库: CREATE DATABASE janes_production;"
echo "   - 创建用户并授权"
echo ""
echo "4. 初始化项目："
echo "   cd $PROJECT_DIR"
echo "   pnpm install"
echo "   pnpm run init-db"
echo "   pnpm run create-admin"
echo ""
echo "5. 配置 Nginx："
echo "   cp $PROJECT_DIR/nginx.conf /etc/nginx/sites-available/janes"
echo "   ln -s /etc/nginx/sites-available/janes /etc/nginx/sites-enabled/"
echo "   nginx -t && systemctl reload nginx"
echo ""
echo "6. 启动应用："
echo "   cd $PROJECT_DIR"
echo "   pm2 start ecosystem.config.js"
echo "   pm2 save"
echo ""
echo "7. 部署前端："
echo "   cd $PROJECT_DIR/frontend"
echo "   pnpm install"
echo "   pnpm run build"
echo "   cp -r dist/* $FRONTEND_DIR/"
echo ""
print_warning "请按照上述步骤完成后续配置"

